
/*Myeditor version 0.0.1
 *Display the file page by page.  */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <curses.h>

int main(int argc, char*argv[])
{
	FILE *fp;
	int ch;
	char *prog = argv[0];
	char row, col;
	int nLine = 0;
	int nColum = 0;
	int nChar = 0;

	int buffer[10000];

	initscr();
	if((fp = fopen(argv[1],"rt")) == NULL)
	{
		/*printw("\n Cannot open file strike anykey exit!");
		getch();*/
		endwin();
		fprintf(stderr,"%s:\n Cannot open file %s!\n",prog, argv[1]);
		exit(1);
	}
	getmaxyx(stdscr,row,col);
	ch = fgetc(fp);
	while(ch != EOF)
	{
		buffer[nChar]= ch;
		nChar++;
		ch=fgetc(fp);
	 /*  	if(ch=='\n')
		{
			nLine++;
		}
		if(nLine==row)
		{
		nLine=0;
		getch();
		clear();
		refresh();
		}
		addch(ch);
		refresh();
		ch=fgetc(fp);*/
	} 
	buffer[nChar]='\0';
	nChar=0;
	ch=buffer[nChar];
	while(ch!='\0')
	{	addch(ch);
		nChar++;
		if(ch=='\n' || !(nChar%col))
		{
			nLine++;
		}
		refresh();
		if(nLine==row)
		{
		nLine=0;
		getch();
		clear();
		refresh();
		}
		
		ch=buffer[nChar];
	}
	cbreak();
	noecho();

	fclose(fp);
	getch();
	endwin();
	exit(EXIT_SUCCESS);
}
